//!!!!!!!!!此处不该这样写，只是临时方案。参照纯函数编程以及ES6的Promise

//封装的网络请求方法
// var wxurl = 'http://61.174.208.14:9090/';
var wxurl = '';
var wxtitle = '';
 
//获取用户信息
function getUserInfo(that, callbs, callbf) {
	//检查用户登录状态获取key，参数分别为：成功回调、失败回调
	//checkUser(function(key){
	uni.login({
		success: function() {
			uni.getUserInfo({
				success: function(res) {
					console.log(res)
					typeof callbs == "function" && callbs(res.userInfo);
				}
			})
		}
	})
	//}, function(e){
	//    typeof callbf == "function" && callbf(e);       //接收到的callbf，继续抛出
	//});
}

function getUserAll(callbs, callbf) {
	uni.login({
		success: function(c) {
			uni.getUserInfo({
				success: function(res) {
					typeof callbs == "function" && callbs(res, c.code);
				}
			})
		},
		fail: function(e) {
			typeof callbf == "function" && callbf(e);
		}
	})
}

//登录微信
function loginUser(callbs, callbf) {
	uni.login({ //1.请求微信登录
		provider: 'weixin', 
		success: function(res) {
			//通过微信code，从服务器请求session。 start//
			// #ifdef APP-PLUS
			var url = '/app/login.jspx';
			var opid = res.authResult.openid;
			var code = res.authResult.openid;
			// #endif
			// #ifndef APP-PLUS
			var url = '/minapps/login.jspx';
			var code = res.code;
			// #endif 
			uni.request({ 
				url: wxurl + url, //拿code请求服务器
				data: {
					code:code
				},
				success: function(res) {
					if (res.data.status==200) {
						typeof callbs == "function" && callbs(res); //获取key成功
						//getUser();          //回调微信用户
					} else if (res.data.status == 2002) {
						//无用户状态，跳转绑定页面
					} else {
						var error = {  
							msg: '与服务器换取session失败，请稍后重试！jy',
							errormsg: res
						}
						typeof callbf == "function" && callbf(error); //获取key失败
					}
				}, 
				fail: function(e) {
					var error = { 
						msg: '与服务器换取session失败。请稍后重试！jy',
						errormsg: e
					}
					typeof callbf == "function" && callbf(error); //获取key失败
				}
			})
			//通过微信code，从服务器请求session。 end//
		},
		fail: function(e) {
			var error = {
				msg: '用户拒绝授权，登录失败！wx',
				errormsg: e
			}
			typeof callbf == "function" && callbf(error); //获取key失败
		}
	})
}

//检查登录状态
function checkUser(callbs, callbf) {
	//1.从DB中取key，用以判断用户登录。
	var key = '';
	try {
		key = uni.getStorageSync('session');
		console.log(key)
	} catch (e) {
		console.log(e);
	}
	//2.如果key为空，说明一次未登录，则请求Login。否则直接执行方法
	if (key == '') {
		//此处执行登录，传两个参数fun，分别为成功和失败
		
		// loginUser(function(k) { //login方法回调回key
		// 	key = k;
		// 	if (key != '') {
		// 		typeof callbs == "function" && callbs(key); //重新获得key成功，执行网络请求
		// 	} else {
		// 		var error = {
		// 			msg: '获取session失败，请稍后重试！jy'
		// 		}
		// 		typeof callbf == "function" && callbf(error); //获取key失败，这里应该不会出出现
		// 	}
		// }, function(e) { //login方法回调回error
		// 	typeof callbf == "function" && callbf(e); //接收到的callbf，继续抛出
		// });
	} else {
		typeof callbs == "function" && callbs(key); //原始有key，执行网络请求
	}
}
//post请求，参数分别为：当前页面、url、参数对象、成功回调、失败回调
function httpPostjqr(that, murl, mpara, callbs, callbf) {
    uni.request({
        url: murl, //用户信息交换接口
        method: 'POST',
        data: mpara,
        success: function(res) {
            if (res.data.status == 200) {
                typeof callbs == "function" && callbs(res);
            } 
        },
        complete: function(e) {
            console.log(e)
        }
    });
}


//post请求，参数分别为：当前页面、url、参数对象、成功回调、失败回调
function httpPost(that, murl, mpara, callbs, callbf) {
	//检查用户登录状态获取key，参数分别为：成功回调、失败回调
	//checkUser(function(key) {
		//此时key应该有值，无值说明用户未给权限
		// var key = '';
		// try {
		// 	key = uni.getStorageSync('session');
		// } catch (e) {
		// 	console.log(e); 
		// }
		// console.log(key)
		uni.request({
			url: wxurl + murl, //用户信息交换接口
			method: 'POST',
			data: JSON.stringify(mpara),
			header: {
				"Content-Type": "text/html;charset=UTF-8",
				// 'Authorization':  key
			},
			success: function(res) {
				if (res.data.status == 200) {
					// if(res.data.rd_session!=''&&res.data.rd_session!=undefined){
					// 	uni.setStorage({
					// 		key: 'session',
					// 		data: res.data.rd_session + '',
					// 		success: function(res) { 
					// 			console.log("session写入成功" + res)
					// 		},
					// 		fail: function(res) {
					// 			console.log("session写入失败" + res)
					// 		},
					// 	})
					// }
					typeof callbs == "function" && callbs(res);
				} else if (res.data.status == 2001) {
					//未登录状态
					try { 
						uni.removeStorageSync('session')
						uni.navigateTo({
							url:'/pages/login/login'
						})
						//httpPost(that, murl, mpara, callbs, callbf);
					}  catch (e) {
						var error = { 
							msg: '删除KEY失败，请稍后重试！wx',
							errormsg: e
						}
						typeof callbf == "function" && callbf(error);
					}
				} else if (res.data.status == 2002) {
					//无用户状态，跳转绑定页面
					//uni.redirectTo({ url: "/pages/user/mobile/mobile" });
				} else {
					var error = {
						msg: 'jy出错：' + res.data.message,
						errormsg: res
					}
					typeof callbs == "function" && callbs(res);
				}
			},
			fail: function(e) {
				var error = {
					msg: '获取数据失败，请稍后重试！jy',
					errormsg: e
				}
				typeof callbf == "function" && callbf(error);
			}
		});
	// }, function(e) {
	// 	typeof callbf == "function" && callbf(e); //接收到的callbf，继续抛出
	// });
}

function httpGet(that, murl, mpara, callbs, callbf) {
	uni.request({
		url: wxurl + murl, //用户信息交换接口
		method: 'GET',
		data: JSON.stringify(mpara),
		header: {
			"Content-Type": "text/html;charset=UTF-8"
		},
		success: function(res) {
			if (res.data.status == 200) {
				typeof callbs == "function" && callbs(res);
			} else {
				var error = {
					msg: 'jy出错：' + res.data.message,
					errormsg: res
				}
				typeof callbs == "function" && callbs(res);
			}
		},
		fail: function(e) {
			var error = {
				msg: '获取数据失败，请稍后重试！jy',
				errormsg: e
			}
			typeof callbf == "function" && callbf(error);
		}
	});
}
function getDates(day,todate){
var dateArry =[];
var dateObj = dateLater(todate, day);
dateArry.push(dateObj)
return dateArry;
}
function getWeek(date) {
    var week;
    if(date.getDay() == 0) week = "星期日"
    if(date.getDay() == 1) week = "星期一"
    if(date.getDay() == 2) week = "星期二"
    if(date.getDay() == 3) week = "星期三"
    if(date.getDay() == 4) week = "星期四"
    if(date.getDay() == 5) week = "星期五"
    if(date.getDay() == 6) week = "星期六"
    return week;
}
function getTime(){
	var myDate = new Date(); //实例一个时间对象；
	return myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds();
}
module.exports.httpGet=httpGet
module.exports.getTime=getTime
module.exports.getWeek=getWeek
module.exports.loginUser = loginUser
module.exports.httpPost = httpPost
module.exports.httpGet = httpGet
module.exports.getUserInfo = getUserInfo
module.exports.getUserAll = getUserAll
module.exports.httpPostjqr=httpPostjqr
